<%@ page import="java.sql.*" %>
<%@ page import="com.example.asa.DatabaseUtil" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>灾情信息列表</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f9f9f9;
      padding: 40px;
    }
    h2 {
      text-align: center;
      color: #333;
    }
    table {
      width: 100%;
      border-collapse: collapse;
      margin-top: 20px;
    }
    th, td {
      padding: 12px;
      border: 1px solid #ccc;
      text-align: center;
    }
    th {
      background-color: #f2f2f2;
    }
    tr:hover {
      background-color: #f7f7f7;
    }
    .container {
      max-width: 1000px;
      margin: auto;
      background-color: white;
      padding: 30px;
      border-radius: 10px;
      box-shadow: 0 0 10px rgba(0,0,0,0.1);
    }
    a.back-link {
      display: inline-block;
      margin-top: 20px;
      color: #007BFF;
      text-decoration: none;
    }
    a.back-link:hover {
      text-decoration: underline;
    }
  </style>
</head>
<body>

<div class="container">
  <h2>灾情信息列表</h2>

  <table>
    <thead>
    <tr>
      <th>编号</th>
      <th>标题</th>
      <th>内容</th>
      <th>等级</th>
      <th>发布时间</th>
      <th>发布人</th>
    </tr>
    </thead>
    <tbody>
    <%
      Connection conn = null;
      PreparedStatement stmt = null;
      ResultSet rs = null;

      try {
        conn = DatabaseUtil.getConnection(); // 确保你有这个工具类
        String sql = "SELECT * FROM disasters ORDER BY publish_time DESC";
        stmt = conn.prepareStatement(sql);
        rs = stmt.executeQuery();

        while (rs.next()) {
    %>
    <tr>
      <td><%= rs.getInt("id") %></td>
      <td><%= rs.getString("title") %></td>
      <td><%= rs.getString("content") %></td>
      <td><%= rs.getString("severity") %></td>
      <td><%= rs.getTimestamp("publish_time") %></td>
      <td><%= rs.getString("publisher") %></td>
    </tr>
    <%
        }
      } catch (SQLException e) {
        e.printStackTrace();
        out.println("<tr><td colspan='6'>数据库查询出错</td></tr>");
      } finally {
        if (rs != null) rs.close();
        if (stmt != null) stmt.close();
        if (conn != null) conn.close();
      }
    %>
    </tbody>
  </table>

  <a href="clviLianPage.jsp" class="back-link">返回首页</a>
</div>

</body>
</html>
